﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Write.Application.Common.Leave;
using MediatR;

namespace B.S.BasicData.Write.Application.Handler.Leave
{
    public class LeaveHandler : IRequestHandler<LeaveCommon, ApiResult<int>>
    {
        private readonly IBaseRepository<leaveModel> leaveRepo;

        public LeaveHandler(IBaseRepository<leaveModel> leaveRepo)
        {
            this.leaveRepo = leaveRepo;
        }

        public async Task<ApiResult<int>> Handle(LeaveCommon request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var info = leaveRepo.GetAll().Where(x=>x.leaveId==request.leaveId).FirstOrDefault();
            if (info==null)
            {
                res.Code=ApiEnum.Error;
                res.Message = "为查找到对应记录";
                return await Task.FromResult(res);
            }
            if(request.Sates==2)
            {
               if(string.IsNullOrEmpty(request.leavecause))
                {
                    res.Code = ApiEnum.Error;
                    res.Message = "请填写原因";
                    return await Task.FromResult(res);
                }
                info.Sates = 2;
                info.leavecause = request.leavecause;
            }

            else if(request.Sates == 1)
            {
                info.Sates = 1;
                info.leavecause =null;
            }
           
            await leaveRepo.Update(info);

            res.Code = ApiEnum.Success;
            res.Message = "审批成功";
            return await Task.FromResult(res);

        }
    }
}
